package com.wwt.springboot_vue.controller;

import com.wwt.springboot_vue.bean.Order;
import com.wwt.springboot_vue.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("findAll")
    public List<Order> findAllGoods(){
        List<Order> list = orderService.findAll();
        return list;
    }

    @GetMapping("findByStatus")
    public List<Order> findByStatus(String info){
        List<Order> list = orderService.findByStatus(info);
        return list;
    }

    @GetMapping("findById")
    public Order findById(int order_id){
        Order order = orderService.findById(order_id);
        return order;
    }

    @GetMapping("findByLimit")
    public List<Order> findByLimit(int start, int size){
        return orderService.findByLimit(start, size);
    }

    @GetMapping("findByStatusLimit")
    public List<Order> findByStatusLimit(String info, int start, int size){
        return orderService.findByStatusLimit(info, start, size);
    }

    @GetMapping("delete")
    public Map<String,Object> delete(int order_id){
        Map<String,Object> map = new HashMap<>();
        try {
            orderService.delete(order_id);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    @PostMapping("add")
    public Map<String,Object>add(int user_id,
                                 int goods_id,
                                 String order_status,
                                 String order_time){
        Map<String,Object> map = new HashMap<>();
        try {
            orderService.add(user_id, goods_id,order_status,order_time);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    @PostMapping("update")
    public Map<String,Object>update(@RequestBody Order order){
        Map<String,Object> map = new HashMap<>();
        try {
            orderService.update(order);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

}
